package udp

import (
	"gitee.com/hasika/common-server/conn/impl_base"
	"gitee.com/hasika/common-server/conn/inet"
	"net"
)

type ClientConnection struct {
	*impl_base.Connection
	UDPConn *net.UDPConn
}

func NewClientConnection(UDPConn *net.UDPConn) *ClientConnection {
	return &ClientConnection{UDPConn: UDPConn, Connection: impl_base.NewBaseConnection()}
}

func (t *ClientConnection) GetRemote() net.Addr {
	return t.UDPConn.RemoteAddr()
}

func (t *ClientConnection) Read(p []byte) (n int, err error) {
	defer func() {
		t.RecordRead(n)
	}()
	return t.UDPConn.Read(p)
}

func (t *ClientConnection) GetConnectionType() inet.ConnectionType {
	return inet.Udp
}

func (t *ClientConnection) LowerWrite(data []byte) error {
	return t.WriteWithLimit(t.UDPConn, data)
}

func (t *ClientConnection) CloseConn() error {
	return t.UDPConn.Close()
}

func (t *ClientConnection) InitFd() {

}
